﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Framework.Common;

namespace BridgeShips.GUISystem {
    public class GUIController : MonoSingleton<GUIController> {
        public PlayerEventHandler Player { get; private set; }

        /// <summary>The main Canvas that's used for the GUI elements.</summary>
        public Canvas Canvas { get { return m_Canvas; } }


        public Font Font { get { return m_Font; } }

        [Header( "Setup" )]

        [SerializeField]
        private Canvas m_Canvas = null;

        [SerializeField]
        private Camera m_GUICamera = null;

        [SerializeField]
        private Font m_Font = null;

        [SerializeField]
        [Reorderable]
        [Tooltip( "If the player clicks while on those rects, the current selection will not be lost." )]
        private ReorderableRectTransformList m_SelectionBlockers = null;

        [Header( "Audio" )]

        [SerializeField]
        private AudioClip m_InventoryOpenClip = null;

        [SerializeField]
        private AudioClip m_InventoryCloseClip = null;



        public bool MouseOverSelectionKeeper() {
            for (int i = 0; i < m_SelectionBlockers.Count; i++) {
                if (!m_SelectionBlockers[i].gameObject.activeSelf)
                    continue;

                bool containsPoint = RectTransformUtility.RectangleContainsScreenPoint( m_SelectionBlockers[i], Input.mousePosition, m_GUICamera );
                if (containsPoint)
                    return true;
            }

            return false;
        }

        public void ApplyForAllCollections() {

        }

        private void Awake() {
            Player = GameController.LocalPlayer;
        }

        private void Start() {
            InventoryController.Instance.State.AddChangeListener( OnChanged_InventoryState );
        }

        private void OnChanged_InventoryState() {
            if (!InventoryController.Instance.IsClosed)
                GameController.Audio.Play2D( m_InventoryOpenClip, 0.6f );
            else
                GameController.Audio.Play2D( m_InventoryCloseClip, 0.6f );
        }
    }
}

